package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"net/http"
	"strconv"
	"strings"
	"time"
)

/*
curl --request GET \
  --url https://sz.lianjia.com/chengjiao/pg1 \
  --header 'cache-control: no-cache' \
  --header 'postman-token: 26248976-2f6e-9286-4096-7fdccb66fc2f'
*/

const (
	ShenZhen  = "sz"
	ZhuHai    = "zh"
	GuangZhou = "gz"
	ZhongShan = "zs"
)

var (
	TableHeader = []string{
		"小区",
		"几室几厅",
		"面积（平米）",
		"朝向",
		"装修",
		"有无电梯",
		"成交日期",
		"成交总价（万）",
		"成交单价（万/平）",
		"楼层高低",
		"楼层总层数",
		"建成年份",
		"建成类型",
		"房龄（房屋满多少年）",
		"是否近地铁",
		"挂牌价（万）",
		"成交周期（天）",
	}
)

func main() {
	fmt.Println(strings.Join(TableHeader, ","))
	getGuangZhouSold()
}

func getGuangZhouSold() {
	for i := 1; i <= 100; i++ {
		getOnePage(GuangZhou, i)
		time.Sleep(time.Second)
	}
}
func getZhongShanSold() {
	for i := 1; i <= 63; i++ {
		getOnePage(ZhongShan, i)
		time.Sleep(time.Second)
	}
}

func getZhuHaiSold() {
	for i := 1; i <= 9; i++ {
		getOnePage(ZhuHai, i)
		time.Sleep(time.Second)
	}
}

func getShenZhenSold() {
	for i := 1; i <= 100; i++ {
		getOnePage(ShenZhen, i)
		time.Sleep(time.Second)
	}
}

func getOnePage(cityCode string, pageNum int) {
	client := &http.Client{}

	url := fmt.Sprintf("https://%s.lianjia.com/chengjiao/pg%d", cityCode, pageNum)

	request, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		panic(err)
	}

	request.Header.Set("cache-control", "no-cache")
	request.Header.Set("postman-token", "26248976-2f6e-9286-4096-7fdccb66fc2f")

	response, err := client.Do(request)
	if err != nil {
		panic(err)
	}

	defer response.Body.Close()

	doc, err := goquery.NewDocumentFromReader(response.Body)
	if err != nil {
		panic(err)
	}

	doc.Find(".info").Each(func(i int, selection *goquery.Selection) {
		//if i > 0 {
		//	return
		//}
		var columns []string

		title := selection.Find(".title").Eq(0).Text()
		titleChild := strings.Split(title, " ")
		if len(titleChild) < 3 {
			//panic(fmt.Errorf("invalid title %s\n", title))
			return
		}

		// 小区
		columns = append(columns, strconv.Quote(strings.Join(titleChild[0:len(titleChild)-2], " ")))
		// 几室几厅
		columns = append(columns, strconv.Quote(titleChild[len(titleChild)-2]))
		// 面积
		columns = append(columns, strconv.Quote(strings.Replace(titleChild[len(titleChild)-1], "平米", "", -1)))

		houseInfoChild := strings.Split(selection.Find(".houseInfo").Eq(0).Text(), "|")
		if len(houseInfoChild) < 3 {
			for i := len(houseInfoChild); i < 3; i++ {
				houseInfoChild = append(houseInfoChild, "")
			}
		}
		// 朝向
		columns = append(
			columns,
			strconv.Quote(strings.Trim(houseInfoChild[0], " ")),
		)
		// 装修情况
		columns = append(
			columns,
			strconv.Quote(strings.Trim(houseInfoChild[1], " \u00a0")),
		)
		// 是否有电梯
		columns = append(
			columns,
			strconv.Quote(strings.Trim(houseInfoChild[2], " ")),
		)

		// 成交时间
		columns = append(columns, strconv.Quote(selection.Find(".dealDate").Eq(0).Text()))
		// 成交总价
		columns = append(columns, strconv.Quote(
			strings.ReplaceAll(selection.Find(".totalPrice").Eq(0).Text(), "万", ""),
		))
		// 成交单价/平
		columns = append(columns, strconv.Quote(strings.ReplaceAll(selection.Find(".unitPrice").Eq(0).Text(), "元/平", "")))

		positionInfoChild := strings.Split(selection.Find(".positionInfo").Eq(0).Text(), " ")
		if len(positionInfoChild) < 2 {
			for i := len(positionInfoChild); i < 3; i++ {
				positionInfoChild = append(positionInfoChild, "")
			}
		}
		// 楼层高中低
		columns = append(columns, strconv.Quote(strings.Split(positionInfoChild[0], "(")[0]))
		// 总楼层数
		columns = append(columns, strconv.Quote(
			SliReplace([]string{"共", "层"}, strings.Split(strings.Split(positionInfoChild[0], "(")[1], ")")[0], ""),
		),
		)

		// 建成年份
		positionInfoChild2 := strings.Split(positionInfoChild[1], "年建")
		if len(positionInfoChild2) < 2 {
			for i := len(positionInfoChild2); i < 2; i++ {
				positionInfoChild2 = append(positionInfoChild2, "")
			}
		}
		columns = append(columns,
			strconv.Quote(positionInfoChild2[0]),
		)
		// 建成类型
		columns = append(columns,
			strconv.Quote(positionInfoChild2[1]),
		)

		// 楼房房龄
		columns = append(columns, strconv.Quote(
			SliReplace([]string{"房屋满", "年"}, selection.Find(".dealHouseTxt").Find("span").Eq(0).Text(), ""),
		))
		// 是否近地铁
		columns = append(columns, strconv.Quote(selection.Find(".dealHouseTxt").Find("span").Eq(1).Text()))

		// 挂牌价
		columns = append(columns, strconv.Quote(
			SliReplace([]string{"挂牌", "万"}, selection.Find(".dealCycleTxt").Find("span").Eq(0).Text(), ""),
		))
		// 成交周期
		columns = append(columns, strconv.Quote(
			SliReplace([]string{"成交周期", "天"}, selection.Find(".dealCycleTxt").Find("span").Eq(1).Text(), ""),
		))

		fmt.Println(strings.Join(columns, ","))
	})
}

func SliReplace(s []string, val string, new string) string {
	for _, v := range s {
		val = strings.ReplaceAll(val, v, new)
	}
	return val
}
